<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'

import { ref } from 'vue'

//宿舍分类数据模型
const categorys = ref([
    {
        "id": 1,
        "categoryName": "置业大厦",
        "categoryPrice": "4800",
        "createTime": "2024-07-31 00:50:45",
        "updateTime": "2024-07-31 01:04:00"
    },
    {
        "id": 2,
        "categoryName": "双创四人间",
        "categoryPrice": "3800",
        "createTime": "2024-07-31 14:20:10",
        "updateTime": "2024-07-31 14:20:10"
    },
    {
        "id": 4,
        "categoryName": "双创双人间",
        "categoryPrice": "8000",
        "createTime": "2024-07-31 22:37:48",
        "updateTime": "2024-07-31 22:37:48"
    },
    {
        "id": 5,
        "categoryName": "71四人间",
        "categoryPrice": "3800",
        "createTime": "2024-07-31 22:39:46",
        "updateTime": "2024-07-31 22:39:46"
    },
    {
        "id": 6,
        "categoryName": "101四人间",
        "categoryPrice": "4500",
        "createTime": "2024-08-01 14:41:40",
        "updateTime": "2024-08-01 14:41:40"
    }
])

//用户搜索时选中的分类id
const categoryId = ref('')

//宿舍列表数据模型
// const dormitory = ref([
//     {
//         "id": 1,
//         "dormitoryId": "801",
//         "categoryId": 2,
//         "createUser": 1,
//         "createTime": "2024-07-31 14:44:19",
//         "updateTime": "2024-07-31 14:50:22"
//     }
// ])

//查询宿舍数据模型
const dormitoryFindPeo = ref([
    {
        "name" : '程宣富',
        "dormitoryId": "801",
        "categoryName":"双创四人间",
        "createTime": "2024-07-31 14:44:19",
        "updateTime": "2024-07-31 14:50:22"
    }
])

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(4)//总条数
const pageSize = ref(4)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    // dormitoryList();
    showByZhiYe();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    // dormitoryList();
    showByZhiYe();
}

/* //宿舍分类数据回显
import { dormitoryCategoryListService } from '@/api/Dormitory.js'
const dormitoryCategory = async () => {
    let result = await dormitoryCategoryListService()
    //测试
    console.log(result.data)

    categorys.value = result.data;
}
dormitoryCategory(); */

// 宿舍列表接口调用
// import { dormitoryListService } from '@/api/Dormitory.js'

// import {StuFindByZhiYeService} from '@/api/Stu.js'

// //根据宿舍id和宿舍名查询宿舍中有哪些学生
// //查询数据

// const findPeo = async(row)=>{
//   dormitoryPeo.dormitoryId = row.dormitoryId;
//   dormitoryPeo.categoryName = row.categoryName;
//   let result = await findPeoService(dormitoryPeo);
//   //TODO
//   //显示result
//   console.log(result.data);
// }



// const dormitoryData = ref({
//     // name:'',
//     id:'',
//     dormitoryId: '',
//     categoryName: '置业大厦',
// })


import { ElMessageBox } from 'element-plus'
import {ElMessage} from 'element-plus'
import {StuUpdateDormitoryByZhiYeService} from '@/api/Stu.js'
import { useStuInfoStore } from '@/stores/stu.js'
const StuInfoStore = useStuInfoStore();
const makeTrue = (row) => {
    //提示用户，确认框
    ElMessageBox.confirm(
        '你确认要选择这个宿舍吗？这个宿舍为'+ row.categoryName + '的' + row.dormitoryId + '号房间',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //调用接口
            dormitoryData.value = row
            dormitoryData.value.id = StuInfoStore.info
            console.log(dormitoryData.value)
            let result = await StuUpdateDormitoryByZhiYeService(dormitoryData.value)

            ElMessage({
                type: 'success',
                message: '选择成功',
            })
            //刷新列表
            showByZhiYe()
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消',
            })
        })
}

import {dormitoryInfoStore} from '@/stores/dormitory.js'
const dormitoryStore = dormitoryInfoStore();

import {categoryNameStore} from '@/stores/categoryName.js'
const categoryName = categoryNameStore();

//根据宿舍id和宿舍名查询宿舍中有哪些学生
const dormitoryPeo = ref({
  dormitoryId:'',
  categoryName:''
})
// })
import {findPeoService} from '@/api/Dormitory.js'

const findPeo = async () => {
    /* {{categoryName.info}}{{ dormitoryStore.info }} */
    dormitoryFindPeo.dormitoryId = dormitoryStore.info,
    dormitoryFindPeo.categoryName = categoryName.info
    let params = {
        pageNum: pageNum.value,//当前页
        pageSize: pageSize.value,//每页条数
        dormitoryId : dormitoryStore.info,
        categoryName : categoryName.info
    }
    /* console.log(dormitoryStore.info)
    console.log(categoryName.info)
    console.log(params) */
    let result = await findPeoService(params);
    // console.log(result.data.items)
    dormitoryFindPeo.value = result.data.items;
    // dormitory.value = result.data;
    // for (let i = 0; i < dormitory.value.length; i++) {
    //     let d = dormitory.value[i];
    //     for (let j = 0; j < categorys.value.length; j++) {
    //         if (d.categoryId == categorys.value[j].id) {
    //             d.categoryName = categorys.value[j].categoryName;
    //         }
    //     }
    // }
    total.value = result.data.total;
}
findPeo();

import { useRouter} from 'vue-router';
const router = useRouter();
import {UseStuInfoStore} from '@/stores/stuInfo.js'
const StuInfo = UseStuInfoStore();
const findStuInfo = (row)=>{
    StuInfo.setInfo(row.name);
    router.push('/Stu/Info');
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span><!-- 分类名称加分类id -->{{categoryName.info}}{{ dormitoryStore.info }}宿舍</span>
                <div class="extra">
                    <!-- <el-button type="primary" @click="visibleDrawer = true; title = '添加宿舍'">添加宿舍</el-button> -->
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <!-- <el-form-item label="宿舍分类：">
        <el-select placeholder="请选择" v-model="categoryId" style="width: 200px">
          <el-option v-for="c in categorys" :key="c.id" :label="c.categoryName" :value="c.id">
          </el-option>
        </el-select>
      </el-form-item> -->
            <el-form-item>
                <!-- <el-button type="primary" @click="dormitoryList()">搜索</el-button>
        <el-button @click="categoryId = ''">重置</el-button> -->
            </el-form-item>
        </el-form>
        <!-- 宿舍列表 -->
        <el-table :data="dormitoryFindPeo" style="width: 100%">
            <el-table-column label="学生姓名" prop="name"></el-table-column>
            <el-table-column label="宿舍编号" prop="dormitoryId"></el-table-column>
            <el-table-column label="分类名称" prop="categoryName"></el-table-column>
            <el-table-column label="查询学生信息按钮">
                <template #default="{ row }">
                    <el-button :icon="prompt" circle plain type="primary" @click="findStuInfo(row)"></el-button>
                </template>
            </el-table-column>
            <!-- <el-table-column label="创建人编号" prop="createUser"></el-table-column> -->
            <el-table-column label="创建时间" prop="createTime"> </el-table-column>
            <el-table-column label="更新时间" prop="updateTime" >
                <!-- <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="makeTrue(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteDormitory(row)"></el-button>
                </template> -->
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="4"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    </el-card>

    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
        <!-- 添加宿舍表单 -->
        <el-form :model="dormitoryModel" label-width="100px">
            <el-form-item label="宿舍编号">
                <el-input v-model="dormitoryModel.dormitoryId" placeholder="请输入宿舍编号"></el-input>
            </el-form-item>
            <!-- <el-form-item label="宿舍价格">
        <el-input v-model="dormitoryModel.dormitoryPrice" placeholder="请输入宿舍价格"></el-input>
      </el-form-item> -->
            <el-form-item label="宿舍分类">
                <el-select placeholder="请选择" v-model="dormitoryModel.categoryId">
                    <el-option v-for="c in categorys" :key="c.id" :label="c.categoryName" :value="c.id">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-button type="primary" @click="title == '添加宿舍' ? addDormitory() : updateDormitory()">确定</el-button>
            <el-button type="info" @click="visibleDrawer = false">取消</el-button>
        </el-form>
    </el-drawer>

</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}

/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}

.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>